package org.nidy.erp.utils;

import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@Slf4j
public class Request {

    public static String get(String url) {
        String responeResult = HttpClient.newHttpClient()
                .sendAsync(
                        HttpRequest.newBuilder()
                                .uri(URI.create(url))
                                .header("Content-Type", "application/json")
                                .GET()
                                .build()
                        , HttpResponse.BodyHandlers.ofString()
                )
                .thenApply(response -> {
                    if (response.statusCode() == 200) {
                        log.info("请求成功：Url{},返回值：{}", url, response.body());
                        return response.body();
                    } else {
                        log.error("请求Url:{},响应状态码：{}", url, response.statusCode());
                        throw new RuntimeException("响应状态码：" + response.statusCode());
                    }
                })
                .exceptionally(e -> {
                    System.err.println("请求异常:" + e.getMessage());
                    log.error("请求异常 Url:{},异常信息：{}", url, e.getMessage());
                    return e.getMessage();
                })
                .join();
        return responeResult;
    }

    public static String post(String url, String jsonBody) {
        String responeResult = HttpClient.newHttpClient()
                .sendAsync(
                        HttpRequest.newBuilder()
                                .uri(URI.create(url))
                                .header("Content-Type", "application/json")
                                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                                .build()
                        , HttpResponse.BodyHandlers.ofString()
                )
                .thenApply(response -> {
                    if (response.statusCode() == 200) {
                        log.info("请求成功：Url{},返回值：{}", url, response.body());
                        return response.body();

                    } else {
                        log.error("请求Url:{},响应状态码：{}", url, response.statusCode());
                        throw new RuntimeException("响应状态码：" + response.statusCode());
                    }
                })
                .thenApply(body -> {              // 替换 thenAccept，返回实际值
                    System.out.println("响应内容: " + body);     // 可选：保留打印逻辑
                    log.info("请求成功：Url{},响应内容：{}", url, body);
                    return body;                  // 返回响应体，供后续使用

                })
                .exceptionally(e -> {
                    System.err.println("请求异常:" + e.getMessage());
                    log.error("请求异常 Url:{},异常信息：{}", url, e.getMessage());
                    return e.getMessage();
                })
                .join();
        return responeResult;
    }
}
